package com.beiye.drivertransport.wxapi;


import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.beiye.drivertransport.R;
import com.beiye.drivertransport.bean.PaymentResultBean;
import com.beiye.drivertransport.config.AppInterfaceConfig;
import com.beiye.drivertransport.utils.Constants;
import com.beiye.drivertransport.utils.MessageEvent;
import com.beiye.drivertransport.utils.TiShiDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

import okhttp3.Call;
import okhttp3.Response;

//微信支付结果
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

    private IWXAPI api;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);

        api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if (resp.errCode == 0) {//标示成功
                Log.i("cc", "支付成功");
                finish();
                SharedPreferences sp1 = getSharedPreferences("msg", Context.MODE_PRIVATE);
                String msg = sp1.getString("msg", "");
                OkGo.get(AppInterfaceConfig.BASE_URL + "sys/firmBill/findState/" + msg)     // 请求方式和请求url
                        .tag(this)                       // 请求的 tag, 主要用于取消对应的请求
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(String result, Call call, Response response) {

                                PaymentResultBean paymentResultBean = JSON.parseObject(result, PaymentResultBean.class);
                                int dataBean = paymentResultBean.getData();
                                if (dataBean == 1)//购买失败
                                {
                                    SharedPreferences sp1 = getSharedPreferences("msg", Context.MODE_PRIVATE);
                                    int paytag = sp1.getInt("paytag", 0);
                                    if (paytag == 1)//首页专项
                                    {
                                        EventBus.getDefault().post(new MessageEvent(MessageEvent.TYPE_1));
                                        SharedPreferences.Editor edit = sp1.edit();
                                        edit.clear();
                                        edit.commit();
                                    } else if (paytag == 2)//全部专题专项
                                    {
                                        EventBus.getDefault().post(new MessageEvent(MessageEvent.TYPE_2));
                                        SharedPreferences.Editor edit = sp1.edit();
                                        edit.clear();
                                        edit.commit();

                                    } else if (paytag == 3)//专项考试
                                    {
                                        EventBus.getDefault().post(new MessageEvent(MessageEvent.TYPE_3));
                                        SharedPreferences.Editor edit = sp1.edit();
                                        edit.clear();
                                        edit.commit();
                                    }
                                    if (paytag == 4)//账户余额
                                    {
                                        EventBus.getDefault().post(new MessageEvent(MessageEvent.TYPE_6));
                                        SharedPreferences.Editor edit = sp1.edit();
                                        edit.clear();
                                        edit.commit();
                                    } else if (paytag == 5)//岗前培训
                                    {
                                        EventBus.getDefault().post(new MessageEvent(MessageEvent.TYPE_7));
                                        SharedPreferences.Editor edit = sp1.edit();
                                        edit.clear();
                                        edit.commit();
                                    } else if (paytag == 6) {//专项培训
                                        EventBus.getDefault().post(new MessageEvent(MessageEvent.TYPE_8));
                                        SharedPreferences.Editor edit = sp1.edit();
                                        edit.clear();
                                        edit.commit();

                                    } else if (paytag == 7) {//违章培训
                                        EventBus.getDefault().post(new MessageEvent(MessageEvent.TYPE_10));
                                        SharedPreferences.Editor edit = sp1.edit();
                                        edit.clear();
                                        edit.commit();


                                    } else if (paytag == 8) {//专项购买次数失败
                                        EventBus.getDefault().post(new MessageEvent(MessageEvent.TYPE_17));
                                        SharedPreferences.Editor edit = sp1.edit();
                                        edit.clear();
                                        edit.commit();
                                    }else if (paytag == 9) {//专项购买次数失败
                                        EventBus.getDefault().post(new MessageEvent(MessageEvent.TYPE_20));
                                        SharedPreferences.Editor edit = sp1.edit();
                                        edit.clear();
                                        edit.commit();
                                    }else if (paytag == 10) { //会员开通失败
                                        EventBus.getDefault().post(new MessageEvent(MessageEvent.TYPE_22));
                                        SharedPreferences.Editor edit = sp1.edit();
                                        edit.clear();
                                        edit.commit();
                                    }

                                } else if (dataBean == 2)//购买成功
                                {
                                    SharedPreferences sp1 = getSharedPreferences("msg", Context.MODE_PRIVATE);
                                    int paytag = sp1.getInt("paytag", 0);
                                    if (paytag == 1) {

                                        EventBus.getDefault().post(new MessageEvent(MessageEvent.TYPE_12));
                                        SharedPreferences.Editor edit = sp1.edit();
                                        edit.clear();
                                        edit.commit();

                                    } else if (paytag == 2) {
                                        EventBus.getDefault().post(new MessageEvent(MessageEvent.TYPE_13));
                                        SharedPreferences.Editor edit = sp1.edit();
                                        edit.clear();
                                        edit.commit();

                                    } else if (paytag == 3)//专项考试
                                    {
                                        EventBus.getDefault().post(new MessageEvent(MessageEvent.TYPE_14));
                                        SharedPreferences.Editor edit = sp1.edit();
                                        edit.clear();
                                        edit.commit();
                                    }
                                    if (paytag == 4)//账户余额
                                    {
                                        EventBus.getDefault().post(new MessageEvent(MessageEvent.TYPE_4));
                                        SharedPreferences.Editor edit = sp1.edit();
                                        edit.clear();
                                        edit.commit();
                                    } else if (paytag == 5)//岗前培训
                                    {
                                        EventBus.getDefault().post(new MessageEvent(MessageEvent.TYPE_5));
                                        SharedPreferences.Editor edit = sp1.edit();
                                        edit.clear();
                                        edit.commit();
                                    } else if (paytag == 6)//专项培训页
                                    {
                                        EventBus.getDefault().post(new MessageEvent(MessageEvent.TYPE_9));
                                        SharedPreferences.Editor edit = sp1.edit();
                                        edit.clear();
                                        edit.commit();

                                    } else if (paytag == 7)//违章培训
                                    {
                                        EventBus.getDefault().post(new MessageEvent(MessageEvent.TYPE_11));
                                        SharedPreferences.Editor edit = sp1.edit();
                                        edit.clear();
                                        edit.commit();

                                    } else if (paytag == 8)//专项购买次数成功
                                    {
                                        EventBus.getDefault().post(new MessageEvent(MessageEvent.TYPE_16));
                                        SharedPreferences.Editor edit = sp1.edit();
                                        edit.clear();
                                        edit.commit();
                                    }else if(paytag==9)//日常培训
                                    {
                                        EventBus.getDefault().post(new MessageEvent(MessageEvent.TYPE_19));
                                        SharedPreferences.Editor edit = sp1.edit();
                                        edit.clear();
                                        edit.commit();
                                    }else if(paytag==10)//会员购买成功
                                    {
                                        EventBus.getDefault().post(new MessageEvent(MessageEvent.TYPE_21));
                                        SharedPreferences.Editor edit = sp1.edit();
                                        edit.clear();
                                        edit.commit();
                                    }

                                }
                            }

                            @Override
                            public void onError(Call call, Response response, Exception e) {
                                super.onError(call, response, e);

                            }
                        });


            } else if (resp.errCode == -1) {
                TiShiDialog.Builder builder1 = new TiShiDialog.Builder(WXPayEntryActivity.this);
                builder1.setMessage(getString(R.string.pay_result_callback_msg, String.valueOf(resp.errCode)));
                builder1.setTitle("提示:");
                builder1.setPositiveButton("我知道", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        SharedPreferences sp1 = getSharedPreferences("msg", Context.MODE_PRIVATE);
                        SharedPreferences.Editor edit = sp1.edit();
                        edit.clear();
                        edit.commit();
                        finish();

                    }
                });
                builder1.create().show();

            } else if (resp.errCode == -2) {
                SharedPreferences sp1 = getSharedPreferences("msg", Context.MODE_PRIVATE);
                SharedPreferences.Editor edit = sp1.edit();
                edit.clear();
                edit.commit();
                finish();

            }


        }

    }

//	@Override
//	protected void onDestroy() {
//		super.onDestroy();
//		SharedPreferences sp1 = getSharedPreferences("msg", Context.MODE_PRIVATE);
//		SharedPreferences.Editor edit = sp1.edit();
//		edit.clear();
//		edit.commit();
//	}
}